<!--
 * @Author: hq
 * @Date: 2022-05-24 10:21:11
 * @LastEditors: hq
 * @LastEditTime: 2022-07-13 15:41:16
 * @Description: 编辑弹窗
 * @version: 1.0
-->
<template>
  <div class="dialog">
    <div class="content">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标题" prop="title">
          <el-input
            v-model="ruleForm.title"
            placeholder="请输入标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="发布人" prop="publisher">
          <el-input
            v-model="ruleForm.publisher"
            placeholder="请输入发布人"
          ></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <EditorBar
            :EditorTxt="EditorTxt"
            :isClear="isClear"
            @getEditorTxt="getEditorTxt"
          ></EditorBar>
        </el-form-item>
        <el-form-item label="摘要" prop="abst">
          <el-input
            v-model="ruleForm.abst"
            placeholder="请输入发布人"
          ></el-input>
        </el-form-item>
        <el-form-item label="发布时间">
          <el-date-picker
            v-model="ruleForm.time"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" :required="true">
          <el-switch
            active-text="禁用"
            inactive-text="启用"
            :active-value="1"
            :inactive-value="0"
            class="tablescope"
            v-model="ruleForm.status"
          >
          </el-switch>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import moment from "moment";
import EditorBar from "@/components/wangEditor.vue";
import { noticeDetail, addAnnounce, editAnnounce } from "@/api/configure";
export default {
  props: ["rowData"],
  components: { EditorBar },
  data() {
    return {
      ruleForm: {
        title: "",
        publisher: "",
        abst: "",
        time: "",
        status: 1,
      },
      isClear: false,
      EditorTxt: "", //富文本内容
      rules: {
        title: [{ required: true, message: "请输入节点名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    async noticeDetail() {
      const { data, err } = await noticeDetail({
        id: this.rowData.id,
      });
      if (err === 0) {
        this.ruleForm.title = data.title;
        this.ruleForm.publisher = data.from;
        this.ruleForm.abst = data.abst;
        this.ruleForm.time = data.time;
        this.ruleForm.status = data.status;
        this.EditorTxt = data.cont;
      }
    },
    //添加
    async addAnnounce() {
      const { msg, err } = await addAnnounce({
        title: this.ruleForm.title,
        from: this.ruleForm.publisher.valueOf(),
        abst: this.ruleForm.abst,
        cont: this.EditorTxt,
        time: this.ruleForm.time,
        status: this.ruleForm.status,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    //编辑
    async editAnnounce() {
      const { msg, err } = await editAnnounce({
        id: this.rowData.id,
        title: this.ruleForm.title,
        from: this.ruleForm.publisher,
        abst: this.ruleForm.abst,
        cont: this.EditorTxt,
        time: this.ruleForm.time,
        status: this.ruleForm.status,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.$emit("handleClose");
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    //富文本
    getEditorTxt(val) {
      this.EditorTxt = val;
    },
    handleSubmit() {
      if (this.rowData.id) {
        //编辑
        this.editAnnounce();
      } else {
        //新增
        this.addAnnounce();
      }
    },
  },
  created() {
    if (this.rowData.id) {
      //编辑
      this.noticeDetail();
    }
  },
};
</script>

<style lang="scss" scoped>
.dialog {
  padding: 0 100px 0 20px;
  box-sizing: border-box;
  height: 100%;
  .content {
    height: 92%;
    overflow: auto;
    &::-webkit-scrollbar {
      width: 0;
    }
  }
  .row {
    display: flex;
    margin-bottom: 20px;
    .col {
      width: 100px;
      display: flex;
      align-items: center;
      justify-content: flex-end;
      flex-shrink: 0;
      margin-right: 10px;
    }
    .red {
      margin-right: 5px;
    }
  }
  .row:nth-child(3) {
    align-items: flex-start;
  }
  .el-input__inner {
    height: 36px;
    line-height: 36px;
  }
  //   .btn {
  //     box-sizing: border-box;
  //     .el-button {
  //       margin-left: 100px;
  //     }
  //   }
}
/deep/.el-tree--highlight-current
  .el-tree-node.is-current
  > .el-tree-node__content {
  .el-tree-node__label {
    color: #2978ff !important;
  }
}
</style>
